package com.cl.blog.web.client.community;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.cl.blog.common.Result;
import com.cl.blog.entity.BCommunityReview;
import com.cl.blog.service.BCommunityReviewService;
import com.cl.blog.vo.CommunityReviewVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 付
 * @Date 2024-04-08 14:46
 * @Version 1.0
 * @description
 * @since 1.0
 */

@RestController
@RequestMapping("/user/review")
public class CommunityReviewController {
	@Autowired
	BCommunityReviewService bCommunityReviewService;

	@PostMapping("/insert")
	public Result insertReview(@RequestBody CommunityReviewVo review, HttpServletRequest request) {
		System.out.println(review);
		// 社区问答id、文章内容、用户id、父级id
		boolean b = bCommunityReviewService.insertReview(review, request);
		if (!b) {
			return Result.error("系统错误，稍后再试");
		}
		return Result.success();
	}

	@GetMapping("/like")
	public Result like(Integer id) {
		BCommunityReview byId = bCommunityReviewService.getById(id);
		Assert.notNull(byId, "抱歉，该评论已删除");
		boolean id1 = bCommunityReviewService.update(new UpdateWrapper<BCommunityReview>()
				.eq("id", id)
				.setSql("like_count=like_count+1")
		);
		if (!id1) {
			return Result.error("系统错误，稍后再试");
		}
		return Result.success();
	}
}
